package com.hit.eduService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hit.eduService.client.VodClient;
import com.hit.eduService.entity.EduVideo;
import com.hit.eduService.mapper.EduVideoMapper;
import com.hit.eduService.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-11-04
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {


    @Autowired
    private VodClient vodClient;
    //根据课程id删除对应的全部小节 删除小节时把对应的视频也删除
    @Override
    public void deleteVideoByCourseId(String courseId) {
        //根据课程id查出对应的所有视频id
        QueryWrapper<EduVideo> wrapperVideos=new QueryWrapper<>();
        wrapperVideos.eq("course_id",courseId);
        wrapperVideos.select("video_source_id");//只查这一个字段而不是select *
        List<EduVideo> eduVideos=baseMapper.selectList(wrapperVideos);
        //把List<EduVideo>转成List<String> 用来调用vodClient的方法
        List<String> videoIds=new ArrayList<>();
        for (int i = 0; i < eduVideos.size(); i++) {//把videoId不空的加入到videoIds中
            String s=eduVideos.get(i).getVideoSourceId();
            if(!StringUtils.isEmpty(s)){
                videoIds.add(s);
            }
        }
        if(videoIds.size()!=0){
            vodClient.deleteBatch(videoIds);
        }

        QueryWrapper<EduVideo> wrapper=new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);

    }
}
